package de.revdev.llt.module.filecardboxes;

import de.revdev.llt.module.learning.LearnStatusFCB;
import de.revdev.llt.module.utility.AbstractFacade;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author f.lindermaier
 */
@Stateless
public class FileCardFacade extends AbstractFacade<FileCard> {

    @PersistenceContext(unitName = "LetsLearnTogetherPU")
    private EntityManager em;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public FileCardFacade() {
        super(FileCard.class);
    }

    public List<FileCard> getUnknownCategorieFileCardsForFCB(FileCardBox fileCardBox, LearnStatusFCB learnStatusFCB) {


        String s = "SELECT e FROM FileCard e WHERE e.fileCardBox = :p AND e NOT IN "
                + "(SELECT p.fileCard FROM LearnStatusFC p WHERE p.learnStatusFCB = :q)";
        return em.createQuery(s).setParameter("p", fileCardBox).setParameter("q", learnStatusFCB).getResultList();

//        CriteriaBuilder cb = em.getCriteriaBuilder();
//        CriteriaQuery<FileCard> cq = cb.createQuery(FileCard.class);
//        Root<FileCard> r = cq.from(FileCard.class);
//        Subquery<FileCard> sub = cq.subquery(FileCard.class);
//        Root<LearnStatusFC> rSub = cq.from(LearnStatusFC.class);
//        ParameterExpression<LearnStatusFCB> pSub = cb.parameter(LearnStatusFCB.class);
//        ParameterExpression<FileCardBox> p = cb.parameter(FileCardBox.class);
//        cq.select(r);
//        sub.select(rSub.get(LearnStatusFC_.fileCard));
//        sub.where(cb.equal(rSub.get(LearnStatusFC_.learnStatusFCB), pSub));
//        cq.where(cb.and(cb.equal(r.get(FileCard_.fileCardBox), p), cb.not(r.in(sub))));
//        return em.createQuery(cq).setParameter(p, fileCardBox).setParameter(pSub, learnStatusFCB).getResultList();
    }
}
